<?php

/**
 * 新增专辑封面模板
 * 
 * @copyright xinwei
 * @since 2012-03-23
 */
/*ini_set( "display_errors",true);
error_reporting(E_ALL); */
include_once($_SERVER["DOCUMENT_ROOT"] . "/core/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/core/admin.inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/include/foodv2/inc.php");

$g_cgival = array();
$g_pro = array();
$g_show = array();


function check_cgi_pro()
{
	global $g_cgival, $g_pro, $g_show;
	
	if(! security_check::check_refer())
	{
		throw new Exception("来路不明");
	}
	
	//模板ID
	cgi::get($g_cgival["template_id"], "template_id", 0);
	$g_cgival["template_id"] = intval($g_cgival["template_id"]);
	
	//模板名称
	cgi::get($g_cgival["template_name"], "template_name", "");
	$g_cgival["template_name"] = string::un_script_code($g_cgival["template_name"]);
	$g_cgival["template_name"] = string::un_html($g_cgival["template_name"]);
	
	//模板图片
	cgi::get($g_cgival["template_image"], "template_image", "");
	$g_cgival["template_image"] = string::un_script_code($g_cgival["template_image"]);
	$g_cgival["template_image"] = string::un_html($g_cgival["template_image"]);
	
	cgi::get($g_cgival["template_image2"], "template_image2", "");
	$g_cgival["template_image2"] = string::un_script_code($g_cgival["template_image2"]);
	$g_cgival["template_image2"] = string::un_html($g_cgival["template_image2"]);
	
	//前台显示状态
	cgi::get($g_cgival["front_status"], "front_status", 0);
	$g_cgival["front_status"] = intval($g_cgival["front_status"]);
}

function get_data()
{
	global $g_cgival, $g_pro, $g_show;

	$db = new db_food_album_cover_template();
	$user_db = new db_food_user();
	$admin_log = new admin_log();
	
	// 拿到当前站点的site_id
	$site_id = admin::admin_get_user_site_id();
	$admin_id = admin::admin_get_login_uid();	// 拿到当前管理员ID
	$admin_name = admin::admin_get_login_user_name();	// 拿到当前管理员用户名

	if ($_FILES['file1'] || $_FILES['file2'])
	{
		try
		{
			if(!empty($_FILES['file1'])){
				/* 上传图片 */
				$upload = new s3_upload();
				$filename_time = time();
				$large = "/cove_template/large/".$filename_time;
				$g_show['data'] = $upload->show($_FILES['file1'])->zomm(710,250)->save($large);

				$g_show['data'] = $g_show['data']['src'];
			}
			if(!empty($_FILES['file2'])){
				/* 上传图片 */
				$upload = new s3_upload();
				$filename_time = time();
				$small = "/cove_template/small/".$filename_time;
				$g_show['data'] = $upload->show($_FILES['file2'])->zomm(160,105)->save($small);

				$g_show['data'] = $g_show['data']['src'];
			}
			
		}
		catch (Exception $e)
		{
			throw new Exception($e->getMessage(), 1);
			
		}
	}
	else
	{
		//重名判断
		$same_name_check = $db->admin_get_food_album_cover_template_list_by_site_id($site_id,$g_cgival["template_name"],false,'',1,1);
		if(!empty($same_name_check))
		{
			throw new Exception("重名，请更换模版名");
		}
		
		//判断新增/更新
		if($g_cgival["template_id"]>0)
		{
			//更新
			$data = $db->admin_insert_album_cover_template_by_id(
				$g_cgival["template_id"],
				$g_cgival["template_name"],
				$g_cgival["template_image"],
				$g_cgival["template_image2"],
				$g_cgival["front_status"],
				$admin_id,
				$admin_name);
			if ($data)
			{
				$admin_log->admin_insert_log_info($admin_id, $site_id, 13, "编辑 专辑模板template_id={$g_cgival["template_id"]}");
			}
		}
		else
		{
			//新增
			$data = $db->admin_insert_album_cover_template(
				$site_id,$g_cgival["template_name"],
				$g_cgival["template_image"],
				$g_cgival["template_image2"],
				$g_cgival["front_status"],
				$admin_id,
				$admin_name);
			
			if ($data)
			{
				$admin_log->admin_insert_log_info($admin_id, $site_id, 13, "新增 专辑模板template_id={$g_cgival["template_id"]}");
			}
		}
		
		if(!$data)
		{
			throw new Exception("操作失败");
		}
	}
	
	
}

$g_show["error"] = 0;
$g_show["errmsg"] = '';
try
{
	admin_check_login();
// 	admin_check_user_priv("foodv2/album_cover_template_add_post.php");
	
	check_cgi_pro();
	get_data();
}
catch(Exception $e)
{
	$g_show["error"] = 1;
	$g_show["errmsg"] = $e->getMessage();
}

show_pro();
exit;


function show_pro()
{
	global $g_cgival, $g_pro, $g_show;
	if(isset($_GET['debug']))
		print_r($g_show);
	//echo output::json('json',$g_show);
	echo json_encode($g_show);
}

?>